package com.oocl.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.oocl.dao.MerchantDao;
import com.oocl.dao.impl.MerchantDaoImpl;
import com.oocl.pojo.Merchant;
import com.oocl.service.MerchantManager;

public class MerchantManagerImpl implements MerchantManager {
	private MerchantDao merchantDao = null;
	
	public MerchantManagerImpl(){
		merchantDao = new MerchantDaoImpl();
	}
	
	
	@Override
	public int addMerchant(Merchant m) {
		int n = merchantDao.addMerchant(m);	
		return n;
	}

	@Override
	public Merchant findOneMerchantByName(String mName) {
		Merchant merchant = merchantDao.findOneMerchantByMName(mName);
		return merchant;
	}
	
	@Override
	public Merchant findOneMerchantById(String mId) {
		Merchant merchant = merchantDao.findOneMerchantByMId(mId);
		return merchant;
	}

	@Override
	public int updateMerchant(Merchant m) {
		int n = merchantDao.updateMerchant(m);
		return n;
	}


	@Override
	public List<Merchant> findMerchantsByMIdList(String Name, Integer dType,
			List<String> mIdList) {
		List<Merchant> ml = new ArrayList<Merchant>();
		Merchant merchant = null;
		if(dType == 0){
			if(Name==null){	//dType=0,dName=null,搜索所有过审非拉黑商家
				for(String mId:mIdList){
					merchant = merchantDao.findOneMerchantByMId(mId);
					if(merchant!=null)ml.add(merchant);
				}
			}
			else{				//dType=0,dName!=null,搜索所有含有名字带dName关键字菜品的过审非拉黑用户
				for(String mId:mIdList){
					merchant = merchantDao.findOneMerchantByDNameAndMId(Name, mId);
					if(merchant!=null)ml.add(merchant);
				}
			}
		}
		else{					
			if(Name==null){	//dType!=0,dName==null,搜索所有含有dType类型菜品的过审非拉黑商家
				for(String mId:mIdList){
					merchant = merchantDao.findOneMerchantByDTypeAndMId(dType, mId);
					if(merchant!=null)ml.add(merchant);
				}
			}
			else{				//dType!=0,dName==!null,搜索所有含有dType类型且名字带dName关键字的菜品的过审非拉黑商家
				for(String mId:mIdList){
					merchant = merchantDao.findOneMerchantByAllThree(Name, dType, mId);
					if(merchant!=null)ml.add(merchant);
				}
			}
		}
		return ml;
	}


	@Override
	public List<String> MerchantsIdFilterByStatus(List<String> mIdList) {
		List<String> mIdListFiltered = new ArrayList<String>();
		String mIdFiltered = null;
		for(String mid : mIdList){
			mIdFiltered = merchantDao.MerchantIdFilterByStatus(mid);
			if(mIdFiltered!=null)mIdListFiltered.add(mIdFiltered);
		}
		return mIdListFiltered;
	}


	

}
